package com.cyh.ebpttoolswar.entitys;

import com.cyh.ebpttoolswar.commmon.StringHelper;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/** @author cyh */
public class Table {
  private String tableName; // 表名称
  private String tableNameUpper; // 表名称(大写)
  private String tableComment; // 表描述
  private String packageName; // 包名
  // 生成辅助
  private String path; // 路径

  private String entityNameLowerName; // 实体名称(小写)
  private String entityName; // 实体名称
  private String entityNameWithEntity; // 实体名称+"Entity"
  private String entityFirstLowerName;
  private String daoName; // dao
  private String daoFirstLowerName;
  private String serviceName; // service
  private String serviceFirstLowerName;
  private String formName; // 表单名称
  private String mapperName; // mapper
  private String controllerName; // controller

  private String actionName; // action
  private String apiName; // 接口
  private String apiUserName; // 用户权限接口

  private String daoPackageName; // dao包名
  private String entityPackageName; // entity
  private String formPackageName; // 表单
  private String mapperPackageName; // mapper
  private String servicePackageName; // service
  private String actionPackageName; // action
  private String apiPackageName; // 接口
  private String apiUserPackageName; // 用户权限接口
  private String pagePackageName; // 页面

  private String actionRequestMapping; // action请求地址
  private String apiRequestMapping; // 接口请地址

  private Date genDateTime = new Date();
  private List<Column> columns = null; // 字段列表

  private int primaryKeyIsNotEmpty;
  private List<Column> primaryColumn = new ArrayList<>(); // 主键列表

  public int getPrimaryKeyIsNotEmpty() {
    return primaryKeyIsNotEmpty;
  }

  public void setPrimaryKeyIsNotEmpty(int primaryKeyIsNotEmpty) {
    this.primaryKeyIsNotEmpty = primaryKeyIsNotEmpty;
  }

  public String getControllerName() {
    return controllerName;
  }

  public void setControllerName(String controllerName) {
    this.controllerName = controllerName;
  }

  public String getPackageName() {
    return packageName;
  }

  public void setPackageName(String packageName) {
    this.packageName = packageName;
  }

  public String getEntityNameWithEntity() {
    return entityNameWithEntity;
  }

  public void setEntityNameWithEntity(String entityNameWithEntity) {
    this.entityNameWithEntity = entityNameWithEntity;
  }
  public String getEntityNameLowerName() {
    return entityNameLowerName;
  }

  public void setEntityNameLowerName(String entityNameLowerName) {
    this.entityNameLowerName = entityNameLowerName;
  }

  // 设置各种路径，freemarker模板中用到
  public void setDefault(Config config) {
    if (tableName == null || tableName.equals("")) {
      return;
    }
    tableNameUpper = tableName.toUpperCase(Locale.ROOT);
    String[] tableNameArry = StringHelper.getSplit(tableName, config.getSplitFlag());
    path = "";
    if (tableNameArry.length > 1) {
      for (int i = 1; i < tableNameArry.length; i++) {
        if (i == 1) {
          entityName = StringHelper.firstToUpper(tableNameArry[i]);

        } else {
          entityName += StringHelper.firstToUpper(tableNameArry[i]);
        }
      }
      path = "." + tableNameArry[0];
    } else {
      entityName = StringHelper.firstToUpper(tableName);
    }
    entityNameWithEntity = entityName+"Entity";
    entityNameLowerName = entityName.toLowerCase(Locale.ROOT);
    entityFirstLowerName = StringHelper.firstToLower(entityName);
    daoName = entityName + config.getDaoName();
    daoFirstLowerName = StringHelper.firstToLower(daoName);
    serviceName = entityName + config.getServiceName();
    serviceFirstLowerName = StringHelper.firstToLower(serviceName);
    mapperName = entityName + config.getMapperName();
    controllerName = entityName + config.getControllerName();
    actionName = entityName + config.getActionName();
    apiName = entityName + config.getApiName();
    apiUserName = entityName + config.getApiUserName();
    formName = entityName + config.getFormName();

    daoPackageName = config.getDaoPackageName() + path;
    entityPackageName = config.getEntityPackageName() + path;
    formPackageName = config.getFormPackageName() + path;
    servicePackageName = config.getServicePackageName() + path;
    mapperPackageName = config.getMapperPackageName() + path;
    actionPackageName = config.getActionPackageName() + path;
    apiPackageName = config.getActionPackageName() + ".api" + path;
    apiUserPackageName = config.getActionPackageName() + ".api.user" + path;
    pagePackageName = "page";
    actionRequestMapping = path.replace(".", "/") + "/" + entityFirstLowerName + "/";
    apiRequestMapping = path.replace(".", "/") + "/api/" + entityFirstLowerName + "/";
  }

  public String getTableName() {
    return tableName;
  }

  public void setTableName(String tableName) {
    this.tableName = tableName;
  }

  public String getTableNameUpper() {
    return tableNameUpper;
  }

  public void setTableNameUpper(String tableNameUpper) {
    this.tableNameUpper = tableNameUpper;
  }

  public String getTableComment() {
    return tableComment;
  }

  public void setTableComment(String tableComment) {
    this.tableComment = tableComment;
  }

  public String getPath() {
    return path;
  }

  public void setPath(String path) {
    this.path = path;
  }

  public String getEntityName() {
    return entityName;
  }

  public void setEntityName(String entityName) {
    this.entityName = entityName;
  }

  public String getEntityFirstLowerName() {
    return entityFirstLowerName;
  }

  public void setEntityFirstLowerName(String entityFirstLowerName) {
    this.entityFirstLowerName = entityFirstLowerName;
  }

  public String getDaoName() {
    return daoName;
  }

  public void setDaoName(String daoName) {
    this.daoName = daoName;
  }

  public String getDaoFirstLowerName() {
    return daoFirstLowerName;
  }

  public void setDaoFirstLowerName(String daoFirstLowerName) {
    this.daoFirstLowerName = daoFirstLowerName;
  }

  public String getServiceName() {
    return serviceName;
  }

  public void setServiceName(String serviceName) {
    this.serviceName = serviceName;
  }

  public String getServiceFirstLowerName() {
    return serviceFirstLowerName;
  }

  public void setServiceFirstLowerName(String serviceFirstLowerName) {
    this.serviceFirstLowerName = serviceFirstLowerName;
  }

  public String getFormName() {
    return formName;
  }

  public void setFormName(String formName) {
    this.formName = formName;
  }

  public String getMapperName() {
    return mapperName;
  }

  public void setMapperName(String mapperName) {
    this.mapperName = mapperName;
  }

  public String getActionName() {
    return actionName;
  }

  public void setActionName(String actionName) {
    this.actionName = actionName;
  }

  public String getApiName() {
    return apiName;
  }

  public void setApiName(String apiName) {
    this.apiName = apiName;
  }

  public String getApiUserName() {
    return apiUserName;
  }

  public void setApiUserName(String apiUserName) {
    this.apiUserName = apiUserName;
  }

  public String getDaoPackageName() {
    return daoPackageName;
  }

  public void setDaoPackageName(String daoPackageName) {
    this.daoPackageName = daoPackageName;
  }

  public String getEntityPackageName() {
    return entityPackageName;
  }

  public void setEntityPackageName(String entityPackageName) {
    this.entityPackageName = entityPackageName;
  }

  public String getFormPackageName() {
    return formPackageName;
  }

  public void setFormPackageName(String formPackageName) {
    this.formPackageName = formPackageName;
  }

  public String getMapperPackageName() {
    return mapperPackageName;
  }

  public void setMapperPackageName(String mapperPackageName) {
    this.mapperPackageName = mapperPackageName;
  }

  public String getServicePackageName() {
    return servicePackageName;
  }

  public void setServicePackageName(String servicePackageName) {
    this.servicePackageName = servicePackageName;
  }

  public String getActionPackageName() {
    return actionPackageName;
  }

  public void setActionPackageName(String actionPackageName) {
    this.actionPackageName = actionPackageName;
  }

  public String getApiPackageName() {
    return apiPackageName;
  }

  public void setApiPackageName(String apiPackageName) {
    this.apiPackageName = apiPackageName;
  }

  public String getApiUserPackageName() {
    return apiUserPackageName;
  }

  public void setApiUserPackageName(String apiUserPackageName) {
    this.apiUserPackageName = apiUserPackageName;
  }

  public String getPagePackageName() {
    return pagePackageName;
  }

  public void setPagePackageName(String pagePackageName) {
    this.pagePackageName = pagePackageName;
  }

  public String getActionRequestMapping() {
    return actionRequestMapping;
  }

  public void setActionRequestMapping(String actionRequestMapping) {
    this.actionRequestMapping = actionRequestMapping;
  }

  public String getApiRequestMapping() {
    return apiRequestMapping;
  }

  public void setApiRequestMapping(String apiRequestMapping) {
    this.apiRequestMapping = apiRequestMapping;
  }

  public Date getGenDateTime() {
    return genDateTime;
  }

  public void setGenDateTime(Date genDateTime) {
    this.genDateTime = genDateTime;
  }

  public List<Column> getColumns() {
    return columns;
  }

  // 设置列信息
  public void setColumns(List<Column> columns) {
    this.columns = columns;
    for (Column c : columns) {
      if (c.getPrimary() == 1) {
        primaryColumn.add(c);
      }
    }
  }

  public List<Column> getPrimaryColumn() {
    return primaryColumn;
  }

  public void setPrimaryColumn(List<Column> primaryColumn) {
    this.primaryColumn = primaryColumn;
  }


}
